package com.edu.solr_news.util;

import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 统计一个文件中各词出现的频率
 */
@Component //交给spring ioc 容器管理
public class CountWordUtil {

    //定义过滤虚词
    private final  String[] filterword = {"the", "a",  "s","an", "that",  "this",  "these",  "those", "i", "me", "my",  "she", "her",  "hers", "his", "he", "him", "his",  "it",  "its",  "we",  "us",  "our",  "ours",   "they",  "them",  "theirs",  "ours", "here",  "there", "in", "into", "to", "for", "by", "on", "at", "with", "upon", "from", "via",  "of", "under",  "above",  "between",  "among",  "as",  "about", "will", "did", "do", "would", "just", "which",  "when",  "self", "are",  "was", "were",  "is", "am",  "have",  "has",  "had",  "and",  "be",  "do",  "did",  "but",  "or",
            "The", "A",  "S","An", "That",  "This",  "These",  "Those", "I", "Me", "My",  "She", "Her",  "Hers", "His", "He", "Him", "His",  "It",  "Its",  "We",  "Us",  "Our",  "Ours",   "They",  "Them",  "Theirs",  "Ours", "Here",  "There", "In", "Into", "To", "For", "By", "On", "At", "With", "Upon", "From", "Via",  "Of", "Under",  "Above",  "Between",  "Among",  "As",  "About", "Will", "Did", "Do",
            "Would", "Just", "Which",  "When",  "Self", "Are",  "Was", "Were",  "Is", "Am",  "Have",  "Has",  "Had",  "And",  "Be",  "Do",  "Did",  "But",  "Or"};

    //虚词过滤
    public  int filter(String word){
        int flag=0; //过滤虚词标记
        for (String s : filterword) {
            if(word.equals(s)) {flag=1;break;}
        }
        return flag;
    }

    //词频统计返回所有结果
    public  Map<String, Integer> wordCount(String text) {
        List<String> lists = new ArrayList<String>();
        String[] words = text.split("[^a-zA-Z]");  //过滤出只含有字母的
        for (String word : words) {
            if (word.length() != 0) {//去除长度为0的行
                if(filter(word)==0)
                lists.add(word);
            }
        }
        Map<String, Integer> wordsCount = new HashMap<String, Integer>();  //存储单词计数信息，key值为单词，value为单词数
        //单词的词频统计
        for (String li : lists) {
            if (wordsCount.get(li) != null) {
                wordsCount.put(li, wordsCount.get(li) + 1);
            } else {
                wordsCount.put(li, 1);
            }

        }
        wordsCount=SortMap(wordsCount);
        return wordsCount;
    }


    //按value的大小进行排序
    public  Map<String,Integer> SortMap(Map<String, Integer> oldmap) {

        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldmap.entrySet());
        Map<String,Integer> newmap=new LinkedHashMap<>();
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return (o2.getValue() - o1.getValue());
            }
        });
        for(int i = 0; i<list.size(); i++){
          newmap.put(list.get(i).getKey(), list.get(i).getValue());
        }
        return newmap;
    }



    //检索页面词频统计结果
    public  String searchWord(String text) {
        Map<String, Integer> stringIntegerMap = new HashMap<>();
        String keyword = "";
        String key = "";
        int count = 0;
        stringIntegerMap = wordCount(text);
        //遍历取前5
        Iterator iterator = stringIntegerMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            key = (String) entry.getKey();
            if (count < 5) {
                keyword = keyword + key + "|";
                count++;
            } else {
                break;
            }
        }
        return keyword;
    }

    //详情页单词列表
    public  Map<String, Integer> listword(String text){
        Map<String, Integer> wordCount = wordCount(text);
        Iterator iterator = wordCount.entrySet().iterator();
        int count=0;
        wordCount=new LinkedHashMap<>();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if(count<20){
                wordCount.put((String) entry.getKey(), (Integer) entry.getValue());
            }else {
                break;
            }
            count++;
        }
        return wordCount;
    }

}

